<?php
/**
 * 减震器 - 数据处理
 */
namespace app\logic\F6DataAnalysis;

use app\server\SendRequestServer;
use think\Exception;
use think\facade\Db;

class ShockAbsorberData implements DataBaseInterface
{

    public function relationship($key): string
    {
        $array = [
            'pro_one' => '减振器',
            'pro_two' => '减振器顶胶',
            'pro_three' => '顶胶轴承',
            'pro_four' => '减振器总成',
            'pro_five' => '减振器防尘套',
            'pro_six' => '减振器缓冲胶',
        ];

        return $array[$key];
    }

    // $data = 前减震器数据
    public function analysis($data, $vin, $room_info = [])
    {
        try {
            return $this->getCarFloorNo($data['车辆详情']);

        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }

//        return [$before, $after];
    }

    public function buildMessage($vin, $car_name, $show)
    {
        return '【查询结果】' . ' \n ' . '🌟' . $vin . '🌟' . ' \n\n ' . '🚘 车名：' . $car_name . ' \n\n ' . $show;
    }

    // 解析车辆详情数据，获取floorNo
    public function getCarFloorNo($data)
    {

        $arr = [];

        $str = '';

        $json = str_replace("'", '"', $data);

        $obj = json_decode($json, JSON_UNESCAPED_UNICODE);


        if (isset($obj[0])) {
            if (isset($obj[0]['floorNo'])) {
                $floorNo = $obj[0]['floorNo'];
                $driveForm = $obj[0]['driveForm'];

                $data = Db::name('project_damping')->where('floor_no', $floorNo)
                    ->withoutField(['id', 'floor_no'])
                    ->find();
                if (! is_null($data)) {

                    $str = '♟️底盘号：' . $floorNo . ' \n ';
                    $str .= '🛻 驱动形式：：' . $driveForm . ' \n ';

                    $str .= '❤️为你找到以下适配商品❤️' . '\n';

                    foreach ($data as $k => $v) {
                        // 如果 $v 存在换行格式数据，则替换换行为 \n
                        if ($v === null) {
                            continue;
                        }
                        $v = str_replace("\n", ' \n ', $v);

                        $kValue = $this->relationship($k);
                        $kValue = "⚡️{$kValue}⚡️" . ' \n '. "【{$kValue}】:" . ' \n ';

                        $str .= $kValue . $v . ' \n ';
                    }
                }
            } else {
                $str = '数据不存在3';
            }
        } else {
            $str = '数据不存在2';
        }

        // 如果为空
        if ($str === '') {
            $str = '😭😭😭【❗️抱歉，暂未找到适配商品❗️】😭😭😭' . ' \n ' . '😄😄【❗️数据已经同步到管理员，会尽快适配❗️】😄😄' . ' \n ' . '👏👏【❗️感谢你的参与❗️】👏👏';
        }

        $arr[] = $str;

        return $arr;
    }

    // 获取后减震器数据
    public function getAfterData($vin)
    {
        try {
            $res = (new SendRequestServer())->send($vin, [
                'module_id' => 32,
                'key' => '后减震器',
                'data_type' => '底盘悬挂系统'
            ]);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }

        // 开始处理，返回后减震器数据
        return $res['data']['车辆详情'];
    }

    // 数据解析
    public function getDataWithRes($data, $name)
    {
        $proArr = [];
        foreach ($data as $k => $v) {
            $text = '';

            if (!isset($v['不同品牌的数据'])) {
                throw new Exception('数据结构错误-3');
            }

            foreach ($v['不同品牌的数据'] as $vv) {
                foreach ($vv as $kkk => $vvv) {

                    // 品牌过滤
                    if (!in_array($kkk,  ['蒙诺/MONROE', '萨克斯/SACHS', 'KYB']))  {
                        continue;
                    }

                    $key = $kkk;
                    $currentPro = $vvv['value'];

                    $text .= '🔖 ' . $name . '品牌：' . $key . ' \n ' . $name . '型号：' . $currentPro . ' \n ' . '======================== \n ';
                }

            }

            $proArr[$k] = $text ?? '未查询到可用数据！';
        }

        return $proArr;
    }
}